//
//  UFOPlayerViewController.m
//  unrivalledForUniverse
//
//  Created by dllo on 16/1/12.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "UFOPlayerViewController.h"
#import <AVFoundation/AVFoundation.h>
#import "LQCAFNetTool.h"
#import "UFOPlayModel.h"
#import "UFOPlayTableView.h"
#import "UFOYZXTypeViewController.h"
#import "UFOheroModel.h"
#import "UFOYZXheroCollectionView.h"
#import "UFOYZXherolistTableView.h"
#import "UFOheroVideoModel.h"
#import "UFOheroVideoPlayModel.h"
#import "UFOYZXVideoTableViewCell.h"
@interface UFOPlayerViewController () <UITableViewDelegate,UITableViewDataSource,UFOYZXheroCollectionViewDelegate>
@property (nonatomic, retain) AVPlayer *player;
@property (nonatomic, retain) UIView *containerV;
@property (nonatomic, retain) UIView *buttonV;
@property (nonatomic, retain) UILabel *timeL;
@property (nonatomic, retain) UIButton *playOrPause;
@property (nonatomic, retain) UIProgressView *progress;
@property (nonatomic, retain) NSMutableArray *playArr;
@property (nonatomic, retain) UITableView *tableV;
@property (nonatomic, retain) UITableView *heroTableV;
@property (nonatomic, retain) NSMutableArray *clearArr;
@property (nonatomic, copy) NSString *play;
@property (nonatomic, retain) UIButton *pauseB;
@property (nonatomic, retain) UIButton *nextB;
@property (nonatomic, retain) UIButton *allB;
@property (nonatomic, retain) UIButton *clearB;
@property (nonatomic, copy) NSString *str;
@property (nonatomic, retain) UIView *titleV;
@property (nonatomic, retain) UIButton *fullB;
@property (nonatomic, retain) UIView *clearV;
@property (nonatomic, retain) UIButton *lookB;
@property (nonatomic, retain) UIView *v;
@property (nonatomic, retain) UITapGestureRecognizer *tapGR;
@property (nonatomic, retain) UIButton *exitB;
@property (nonatomic, retain) UISlider *slider;
@property (nonatomic, retain) UILabel *progressL;
@property (nonatomic, retain) UILabel *videoNameL;
@property (nonatomic, retain) NSMutableArray *heroArr;
@property (nonatomic, retain) UIButton *blowB;
@property (nonatomic, retain) NSMutableArray *heroVideoArr;
@property (nonatomic, retain) NSMutableArray *heroVideoPlayArr;
@property (nonatomic, retain) UFOheroVideoModel *heroVideomodel;
@property (nonatomic, retain) UFOheroModel *Hmodel;
@property (nonatomic, retain) UIView *downloadV;
@property (nonatomic, retain) NSMutableArray *hotVideoArr;
@property (nonatomic, retain) NSMutableArray *hotVideoPlayArr;
@property (nonatomic, retain) UIButton *cacheB;
@property (nonatomic, retain) UIButton *shareB;
@property (nonatomic, retain) UIButton *collectB;
@end

@implementation UFOPlayerViewController
- (void)dealloc{
    [self removeObserverFromPlayerItem:self.player.currentItem];
    [self removeNotification];
    self.tableV.delegate = nil;
    self.tableV.dataSource = nil;
    [_player release];
    [_containerV release];
    [_timeL release];
    [_playArr release];
    [_tableV release];
    [_play release];
    [_titleV release];
    [_progress release];
    [_videoNameL release];
    [_heroArr release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self getHeroData];
    [self getNewData];
    // 视频容器视图
    self.containerV = [[UIView alloc] initWithFrame:CGRectMake(0, 20 * SCREENHEIGHT / HEIGHTSIXP, SCREENWIDTH,SCREENHEIGHT / 3)];
    self.containerV.backgroundColor = [UIColor blackColor];
    [self.view addSubview:self.containerV];
    [self.containerV release];
    
    self.downloadV = [[UIView alloc] initWithFrame:CGRectMake(0, 242 * SCREENHEIGHT / HEIGHTSIXP, SCREENWIDTH, 80 * SCREENHEIGHT / HEIGHTSIXP)];
    self.downloadV.backgroundColor = [UIColor colorWithRed:0.289 green:0.049 blue:0.713 alpha:1.000];
    [self.view addSubview:self.downloadV];
    
    // 播放按钮视图
    self.buttonV = [[UIView alloc] initWithFrame:CGRectMake(0, 200 * SCREENHEIGHT / HEIGHTSIXP, SCREENWIDTH, 40 * SCREENHEIGHT / HEIGHTSIXP)];
    self.buttonV.backgroundColor = [UIColor colorWithWhite:0.342 alpha:0.440];
    [self.view addSubview:self.buttonV];
    [self.buttonV release];
    
    // 退出视频按钮视图
    self.titleV = [[UIView alloc] initWithFrame:CGRectMake(0, 20 * SCREENHEIGHT / HEIGHTSIXP, SCREENWIDTH, 40 * SCREENHEIGHT / HEIGHTSIXP)];
    self.titleV.backgroundColor = [UIColor colorWithWhite:0.342 alpha:0.440];
    [self.view addSubview:self.titleV];
    [self.titleV release];
    
    self.videoNameL = [[UILabel alloc] initWithFrame:CGRectMake(50 * SCREENWIDTH / WIDTHSIXP, 5 * SCREENHEIGHT / HEIGHTSIXP, SCREENWIDTH - 10, 40 * SCREENHEIGHT / HEIGHTSIXP)];
    self.videoNameL.textColor = [UIColor whiteColor];
    self.videoNameL.font = [UIFont systemFontOfSize:12.0];
    self.videoNameL.text = self.videoName;
    [self.titleV addSubview:self.videoNameL];
    [self.videoNameL release];
    
    [self getData];
    self.str = @"流畅";
    [self createButton];
    
    // 轻拍手势
    self.tapGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    [self.containerV addGestureRecognizer:self.tapGR];
    [self.tapGR release];
    
    self.progress = [[UIProgressView alloc] initWithFrame:CGRectMake(115 *SCREENWIDTH / WIDTHSIXP, 24 *SCREENHEIGHT / HEIGHTSIXP, 170 * SCREENWIDTH / WIDTHSIXP, 10 * SCREENHEIGHT / HEIGHTSIXP)];
    self.progress.trackTintColor = [UIColor whiteColor];
    self.progress.progressTintColor = [UIColor lightGrayColor];
    [self.buttonV addSubview:self.progress];
    [_progress release];
    
    self.slider = [[UISlider alloc] initWithFrame:CGRectMake(110 * SCREENWIDTH / WIDTHSIXP, 20 * SCREENHEIGHT / HEIGHTSIXP, 170 * SCREENWIDTH / WIDTHSIXP, 10 * SCREENHEIGHT / HEIGHTSIXP)];
    self.slider.maximumTrackTintColor = [UIColor clearColor];
    [self.slider setThumbImage:[UIImage imageNamed:@"xiaoyuandian"] forState:UIControlStateNormal];
    [self.slider addTarget:self action:@selector(sliderAction:) forControlEvents:UIControlEventValueChanged];
    [self.buttonV addSubview:self.slider];
    [_slider release];
    
    self.progressL = [[UILabel alloc] initWithFrame:CGRectMake(70 * SCREENWIDTH / WIDTHSIXP, 20 * SCREENHEIGHT / HEIGHTSIXP, 40 * SCREENWIDTH / WIDTHSIXP, 10 * SCREENHEIGHT / HEIGHTSIXP)];
    self.progressL.text = @"00:00";
    self.progressL.font = [UIFont systemFontOfSize:10.0];
    self.progressL.textColor = [UIColor whiteColor];
    self.progressL.textAlignment = NSTextAlignmentCenter;
    [self.buttonV addSubview:self.progressL];
    [self.progressL release];
    
    [self createTableView];

}

#pragma 退出视频播放时调用
- (void)viewDidDisappear:(BOOL)animated{
    [self.player pause];
    self.play = nil;
    [super viewDidDisappear:animated];
}

#pragma 创建TableView
- (void)createTableView{

    self.tableV = [[UITableView alloc] initWithFrame:CGRectMake(0, 300 * SCREENHEIGHT / HEIGHTSIXP, SCREENWIDTH, SCREENHEIGHT - 50) style:UITableViewStylePlain];
    self.tableV.backgroundColor = [UIColor colorWithRed:0.339 green:0.181 blue:0.632 alpha:1.000];
    self.tableV.delegate = self;
    self.tableV.dataSource = self;
    [self.view addSubview:self.tableV];
    [_tableV release];
   
}

#pragma 拖动视频
- (void)sliderAction:(UISlider *)sender{
    // 关联视频进度
    [self.player seekToTime:CMTimeMakeWithSeconds(sender.value * CMTimeGetSeconds(self.player.currentItem.duration), 1)];
   
}

#pragma 创建button
- (void)createButton{
    // 暂停按钮
    self.pauseB = [UIButton buttonWithType:UIButtonTypeCustom];
    self.pauseB.frame = CGRectMake(0 * SCREENWIDTH / WIDTHSIXP, 0 * SCREENHEIGHT / HEIGHTSIXP, 50 * SCREENWIDTH / WIDTHSIXP, 50 * SCREENHEIGHT / HEIGHTSIXP);
    [self.pauseB setImage:[UIImage imageNamed:@"zanting"] forState:UIControlStateNormal];
    [self.pauseB addTarget:self action:@selector(pauseBAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.buttonV addSubview:self.pauseB];
    
    // 切换视频按钮
    self.nextB = [UIButton buttonWithType:UIButtonTypeCustom];
    self.nextB.frame = CGRectMake(30 * SCREENWIDTH / WIDTHSIXP, 0 * SCREENHEIGHT / HEIGHTSIXP, 50 * SCREENWIDTH / WIDTHSIXP, 50 * SCREENHEIGHT / HEIGHTSIXP);
    [self.nextB setImage:[UIImage imageNamed:@"xiayiqu"] forState:UIControlStateNormal];
    [self.nextB addTarget:self action:@selector(nextBAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.buttonV addSubview:self.nextB];
    
    self.lookB = [UIButton buttonWithType:UIButtonTypeCustom];
    self.lookB.frame = CGRectMake(270 * SCREENWIDTH / WIDTHSIXP, 0 * SCREENHEIGHT / HEIGHTSIXP, 80 * SCREENWIDTH / WIDTHSIXP, 50 * SCREENHEIGHT / HEIGHTSIXP);
    [self.lookB setTitle:@"清晰度" forState:UIControlStateNormal];
    self.lookB.titleLabel.font = [UIFont systemFontOfSize:13.0];
    [self.lookB addTarget:self action:@selector(lookBAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.buttonV addSubview:self.lookB];
    
    self.fullB = [UIButton buttonWithType:UIButtonTypeCustom];
    self.fullB.frame = CGRectMake(330 * SCREENWIDTH / WIDTHSIXP, 0 * SCREENHEIGHT / HEIGHTSIXP, 50 * SCREENWIDTH / WIDTHSIXP, 50 * SCREENHEIGHT / HEIGHTSIXP);
    [self.fullB setImage:[UIImage imageNamed:@"jinruquanping"] forState:UIControlStateNormal];
    [self.fullB addTarget:self action:@selector(fullBAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.buttonV addSubview:self.fullB];
    
    self.exitB = [UIButton buttonWithType:UIButtonTypeCustom];
    self.exitB.frame = CGRectMake(0, 0, 50 * SCREENWIDTH / WIDTHSIXP, 50 * SCREENHEIGHT / HEIGHTSIXP);
    [self.exitB setImage:[UIImage imageNamed:@"tuichu"] forState:UIControlStateNormal];
    [self.exitB addTarget:self action:@selector(exitBAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.titleV addSubview:self.exitB];
    
    UILabel *namelabel = [[UILabel alloc] initWithFrame:CGRectMake(10 * SCREENWIDTH / WIDTHSIXP, 0, 200 * SCREENWIDTH / WIDTHSIXP, 60 *SCREENHEIGHT / HEIGHTSIXP)];
    namelabel.text = @"解说/搞笑/综合/英雄";
    namelabel.textColor = [UIColor colorWithWhite:0.818 alpha:1.000];
    [self.downloadV addSubview:namelabel];
    [namelabel release];
    
    self.collectB = [UIButton buttonWithType:UIButtonTypeCustom];
    self.collectB.frame = CGRectMake( 220 *SCREENWIDTH / WIDTHSIXP, 0, 60 *SCREENWIDTH / WIDTHSIXP, 60 * SCREENHEIGHT / HEIGHTSIXP);
    [self.collectB setImage:[UIImage imageNamed:@"shoucangcang"] forState:UIControlStateNormal];
    [self.downloadV addSubview:self.collectB];
    
    self.shareB = [UIButton buttonWithType:UIButtonTypeCustom];
    self.shareB.frame = CGRectMake( 270 *SCREENWIDTH / WIDTHSIXP, 0, 60 *SCREENWIDTH / WIDTHSIXP, 60 * SCREENHEIGHT / HEIGHTSIXP);
    [self.shareB setImage:[UIImage imageNamed:@"fengxiangxiang"] forState:UIControlStateNormal];
    [self.downloadV addSubview:self.shareB];
    
    self.cacheB = [UIButton buttonWithType:UIButtonTypeCustom];
    self.cacheB.frame = CGRectMake( 320 *SCREENWIDTH / WIDTHSIXP, 0, 60 *SCREENWIDTH / WIDTHSIXP, 60 * SCREENHEIGHT / HEIGHTSIXP);
    [self.cacheB setImage:[UIImage imageNamed:@"xiazaizai"] forState:UIControlStateNormal];
    [self.downloadV addSubview:self.cacheB];
}

#pragma 进入全屏
- (void)fullBAction:(UIButton *)sender{


}

#pragma 点击退出视频方法
- (void)exitBAction:(UIButton *)sender{
    [self dismissViewControllerAnimated:YES completion:^{
        
        
    }];
}

#pragma 轻拍隐藏播放按钮视图与退出视频按钮视图
- (void)tapAction:(UITapGestureRecognizer *)sender{

    if (self.buttonV.hidden == YES && self.titleV.hidden == YES) {
        
        self.buttonV.hidden = NO;
        self.titleV.hidden = NO;
    }
    else if (self.buttonV.hidden == NO && self.titleV.hidden == NO) {
        
        self.buttonV.hidden = YES;
        self.titleV.hidden = YES;
    }
}

#pragma 清晰度button
- (void)lookBAction:(UIButton *)sender{
    sender.selected = !sender.selected;
    if (sender.selected == YES) {
        // 调用小盒子视图
        [self createView];
    }
    if (sender.selected == NO) {
        // 点击隐藏视图
        self.v.hidden = YES;
    }
    

}

#pragma 流畅高清模式按钮
- (void)allBAction:(UIButton *)sender{
    
    if (sender.currentTitle == self.str) {
        [self.lookB setTitle:self.str forState:UIControlStateNormal];
        [self.lookB setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
        self.v.hidden = YES;
        [self getData];
        NSLog(@"_____________流畅");
    }
    else
    {
        [self.lookB setTitle:@"高清" forState:UIControlStateNormal];
        [self.lookB setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
        self.v.hidden = YES;
        [self getToData];
        NSLog(@"$$$$$$$$$$$高清");
    }
   
}

#pragma 创建清晰度视图
- (void)createView{

    self.v = [[UIView alloc] initWithFrame:CGRectMake(300 * SCREENWIDTH / WIDTHSIXP, 220 * SCREENHEIGHT / HEIGHTSIXP, 0, 0)];
    self.v.backgroundColor = [UIColor colorWithWhite:0.390 alpha:0.820];
    [self.view addSubview:self.v];
    
    self.allB = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.allB setTitle:@"流畅" forState:UIControlStateNormal];
    self.allB.titleLabel.font = [UIFont systemFontOfSize:15.0];
    [self.allB addTarget:self action:@selector(allBAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.v addSubview:self.allB];
    
    self.clearB = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.clearB setTitle:@"高清" forState:UIControlStateNormal];
    self.clearB.titleLabel.font = [UIFont systemFontOfSize:15.0];
    [self.clearB addTarget:self action:@selector(allBAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.v addSubview:self.clearB];
    // 弹出小盒子动画
    [UIView animateWithDuration:0.2 animations:^{
        
        self.v.frame = CGRectMake(270 *SCREENWIDTH / WIDTHSIXP, 100 * SCREENHEIGHT / HEIGHTSIXP, 80 * SCREENWIDTH / WIDTHSIXP, 100 * SCREENHEIGHT / HEIGHTSIXP);
        self.allB.frame = CGRectMake(10 * SCREENWIDTH / WIDTHSIXP, 10 * SCREENHEIGHT / HEIGHTSIXP, 60 * SCREENWIDTH / WIDTHSIXP, 40 * SCREENHEIGHT / HEIGHTSIXP);
        self.clearB.frame = CGRectMake(10 * SCREENWIDTH / WIDTHSIXP, 60 * SCREENHEIGHT / HEIGHTSIXP, 60 * SCREENWIDTH / WIDTHSIXP, 40 * SCREENHEIGHT / HEIGHTSIXP);
    }];
}

#pragma mark - 私有方法
-(void)AVPlaylayerToPlay{
    //创建播放器层
    AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:self.player];
    playerLayer.frame = CGRectMake(0, 0, SCREENWIDTH, SCREENHEIGHT / 3);
    [self.containerV.layer addSublayer:playerLayer];
}

#pragma 初始化播放器
-(AVPlayer *)player{
    if (!_player) {
        AVPlayerItem *playerItem=[self getPlayItem:0];
        _player=[AVPlayer playerWithPlayerItem:playerItem];
        [self addProgressObserver];
        [self addObserverToPlayerItem:playerItem];
    }
    return _player;
}

#pragma 获取播放内容
-(AVPlayerItem *)getPlayItem:(int)videoIndex{
    NSString *urlStr = self.play;
    NSURL *url=[NSURL URLWithString:urlStr];
    AVPlayerItem *playerItem=[AVPlayerItem playerItemWithURL:url];
    return playerItem;
}
#pragma mark - 通知
-(void)addNotification{
    //给AVPlayerItem添加播放完成通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playbackFinished:) name:AVPlayerItemDidPlayToEndTimeNotification object:self.player.currentItem];
}

-(void)removeNotification{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

#pragma 播放完成通知
-(void)playbackFinished:(NSNotification *)notification{
    NSLog(@"视频播放完成.");
}

#pragma mark - 监控
-(void)addProgressObserver{
    AVPlayerItem *playerItem=self.player.currentItem;
    UISlider *slider =self.slider;
    //这里设置每秒执行一次
    [self.player addPeriodicTimeObserverForInterval:CMTimeMake(1.0, 1.0) queue:dispatch_get_main_queue() usingBlock:^(CMTime time) {
        float current=CMTimeGetSeconds(time);
        float total=CMTimeGetSeconds([playerItem duration]);
        NSLog(@"当前已经播放%.2fs.",current);
        if (current) {
            [slider setValue:(current/total) animated:YES];
            NSInteger currentTime = (int)current;
            NSInteger m = currentTime / 60;
            NSInteger s = currentTime % 60;
            NSString *strM =nil;
            NSString *strS =nil;
            if (m < 10) {
                strM = [NSString stringWithFormat:@"0%ld",m];
            } else {
                strM = [NSString stringWithFormat:@"%ld", m];
            }
            
            if (s < 10) {
                strS = [NSString stringWithFormat:@"0%ld", s];
            } else {
                strS = [NSString stringWithFormat:@"%ld", s];
            }
            self.progressL.text = [NSString stringWithFormat:@"%@:%@",strM, strS];
            self.progressL.font = [UIFont systemFontOfSize:10.0];
            self.progressL.textColor = [UIColor whiteColor];
            self.progressL.textAlignment = NSTextAlignmentCenter;
        }
    }];
}

-(void)addObserverToPlayerItem:(AVPlayerItem *)playerItem{
    //监控状态属性，注意AVPlayer也有一个status属性，通过监控它的status也可以获得播放状态
    [playerItem addObserver:self forKeyPath:@"status" options:NSKeyValueObservingOptionNew context:nil];
    //监控网络加载情况属性
    [playerItem addObserver:self forKeyPath:@"loadedTimeRanges" options:NSKeyValueObservingOptionNew context:nil];
}
-(void)removeObserverFromPlayerItem:(AVPlayerItem *)playerItem{
    [playerItem removeObserver:self forKeyPath:@"status"];
    [playerItem removeObserver:self forKeyPath:@"loadedTimeRanges"];
}

#pragma 监控播放状态
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{
    AVPlayerItem *playerItem=object;
    if ([keyPath isEqualToString:@"status"]) {
        AVPlayerStatus status = [[change objectForKey:@"new"] intValue];
        if(status == AVPlayerStatusReadyToPlay){
//            NSLog(@"正在播放...，视频总长度:%.2f",CMTimeGetSeconds(playerItem.duration));
        }
    }else if([keyPath isEqualToString:@"loadedTimeRanges"]){
        NSArray *array = playerItem.loadedTimeRanges;
        CMTimeRange timeRange = [array.firstObject CMTimeRangeValue];//本次缓冲时间范围
        float startSeconds = CMTimeGetSeconds(timeRange.start);
        float durationSeconds = CMTimeGetSeconds(timeRange.duration);
        NSTimeInterval totalBuffer = startSeconds + durationSeconds;//缓冲总长度
        NSLog(@"共缓冲：%.2f",totalBuffer);
        self.progress.progress = totalBuffer / CMTimeGetSeconds(playerItem.duration);
    }
}

#pragma mark - UI事件
- (void)pauseBAction:(UIButton *)sender {

    if(self.player.rate==0){ //说明时暂停
        [sender setImage:[UIImage imageNamed:@"pubause"] forState:UIControlStateNormal];
        [self.player play];
    }else if(self.player.rate==1){//正在播放
        [self.player pause];
        [sender setImage:[UIImage imageNamed:@"bofang"] forState:UIControlStateNormal];
    }
}

#pragma 切换视频方法
- (void)nextBAction:(UIButton *)sender {
    [self removeNotification];
    [self removeObserverFromPlayerItem:self.player.currentItem];
    AVPlayerItem *playerItem=[self getPlayItem:(int)sender.tag];
    [self addObserverToPlayerItem:playerItem];
    //切换视频
    [self.player replaceCurrentItemWithPlayerItem:playerItem];
    [self addNotification];
}

#pragma 切换视频方法
- (void)changeVideoUrl {
    [self removeNotification];
    [self removeObserverFromPlayerItem:self.player.currentItem];
    AVPlayerItem *playerItem=[self getPlayItem:0];
    [self addObserverToPlayerItem:playerItem];
    //切换视频
    [self.player replaceCurrentItemWithPlayerItem:playerItem];
    [self addNotification];
}

#pragma 获取流畅数据
- (void)getData{

    self.playArr = [NSMutableArray array];
    NSString *url =  [NSString stringWithFormat:@"http://box.dwstatic.com/apiVideoesNormalDuowan.php?action=f&vid=%@",self.videoId];
    
    [LQCAFNetTool getNetWithURL:url body:nil headFile:nil responseStyle:LQCJSON success:^(NSURLSessionDataTask *task, id responseObject) {
        
        NSMutableDictionary *dic = responseObject;
        NSMutableDictionary *resultDic = [dic objectForKey:@"result"];
        NSMutableDictionary *itemsDic = [resultDic objectForKey:@"items"];
            NSMutableDictionary *transDic = [itemsDic objectForKey:@"350"];
        NSMutableDictionary *urlsDic = [transDic objectForKey:@"transcode"];
            UFOPlayModel *model = [[UFOPlayModel alloc] initWithDic:urlsDic];
        
            [self.playArr addObject:model];
            self.play = [model.urls objectAtIndex:0];
        [self AVPlaylayerToPlay];
        [self.player play];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
    }];
}

#pragma 获取高清数据
- (void)getToData{

    self.clearArr = [NSMutableArray array];
    NSString *url =  [NSString stringWithFormat:@"http://box.dwstatic.com/apiVideoesNormalDuowan.php?action=f&vid=%@",self.videoId];
    
    [LQCAFNetTool getNetWithURL:url body:nil headFile:nil responseStyle:LQCJSON success:^(NSURLSessionDataTask *task, id responseObject) {
        
        NSMutableDictionary *dic = responseObject;
        NSMutableDictionary *resultDic = [dic objectForKey:@"result"];
        NSMutableDictionary *itemsDic = [resultDic objectForKey:@"items"];
        NSMutableDictionary *transDic = [itemsDic objectForKey:@"1000"];
        NSMutableDictionary *urlsDic = [transDic objectForKey:@"transcode"];
        UFOPlayModel *model = [[UFOPlayModel alloc] initWithDic:urlsDic];
        
        [self.clearArr addObject:model];
        self.play = [model.urls objectAtIndex:0];
        [self AVPlaylayerToPlay];
        [self.player play];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
    }];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    if (tableView == self.tableV) {
        if (0 == indexPath.section) {
            [self getToData];
        } else if (1 == indexPath.section) {
            self.heroVideomodel = [self.hotVideoArr objectAtIndex:indexPath.row];
            [self getNewVIdeoData];
        }
    } else {
        self.heroVideomodel = [self.heroVideoArr objectAtIndex:indexPath.row];
        [self getPlayheroData];
    }
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (tableView == self.tableV) {
        if (0 == section) {
            return 1;
        } else if (1 == section) {
            return self.hotVideoArr.count;
        }
    } else {
        if (0 == section) {
            return self.heroVideoArr.count;
        }
    }
    return 0;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    if (tableView == self.tableV) {
        if (0 == section) {
            return @"英雄视频";
        } else if (1 == section) {
        return @"热门视频";
        }
    } else {
        if (0 == section) {
            return @"热门视频";
        }
    }
    return nil;
}

#pragma 自定义区标题
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    if (tableView == self.heroTableV) {
        UIView *view = [[[UIView alloc] initWithFrame:CGRectMake(10, 0, SCREENWIDTH, 30)] autorelease];
        view.backgroundColor = [UIColor colorWithRed:0.451 green:0.490 blue:0.500 alpha:1.000];
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 200, 30)];
        label.text = @"英雄视频";
        label.textColor = [UIColor whiteColor];
        [view addSubview:label];
        [label release];
        
        self.blowB = [UIButton buttonWithType:UIButtonTypeCustom];
        self.blowB.frame = CGRectMake(320 * SCREENWIDTH / WIDTHSIXP, 0, 50 * SCREENWIDTH / WIDTHSIXP, 30 * SCREENHEIGHT / HEIGHTSIXP);
        [self.blowB setImage:[UIImage imageNamed:@"xialala"] forState:UIControlStateNormal];
        [self.blowB addTarget:self action:@selector(blowBAction:) forControlEvents:UIControlEventTouchUpInside];
        [view addSubview:self.blowB];
        return view;
    }
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (tableView == self.tableV) {
        if (0 == indexPath.section) {
            return 180 * SCREENHEIGHT / HEIGHTSIXP;
        } else if (1 == indexPath.section) {
            return 100 * SCREENHEIGHT / HEIGHTSIXP;
        }
    } else {
        return 100 * SCREENHEIGHT / HEIGHTSIXP;
    }
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    // 自定义collectionView的第一个cell
    if (tableView == self.tableV) {
        if (0 == indexPath.row && 0 == indexPath.section) {
            UFOPlayTableView *cell = [[[UFOPlayTableView alloc] init] autorelease];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.contactArr = self.heroArr;
            cell.delegate = self;
            return cell;
        } else if (1 == indexPath.section) {
        static NSString *cellStr = @"cellvideo";
            UFOYZXVideoTableViewCell *cellvideo = [tableView dequeueReusableCellWithIdentifier:cellStr];
            if (nil == cellvideo) {
                cellvideo = [[UFOYZXVideoTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellStr];
                self.heroVideomodel = [self.hotVideoArr objectAtIndex:indexPath.row];
                [cellvideo.videoV sd_setImageWithURL:[NSURL URLWithString:self.heroVideomodel.P] placeholderImage:[UIImage imageNamed:@"zanweitu"]];
                cellvideo.videotitleL.text = self.heroVideomodel.T;
                cellvideo.timeL.text = self.heroVideomodel.A;
            }
            return cellvideo;
        }
    } else {
        static NSString *cellStr = @"celll";
        UFOYZXherolistTableView *cell = [tableView dequeueReusableCellWithIdentifier:cellStr];
        if (nil == cell) {
            cell = [[UFOYZXherolistTableView alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellStr];
            self.heroVideomodel = [self.heroVideoArr objectAtIndex:indexPath.row];
            [cell.priceV sd_setImageWithURL:[NSURL URLWithString:self.heroVideomodel.P] placeholderImage:[UIImage imageNamed:@"zanweitu"]];
            cell.nameL.text = self.heroVideomodel.T;
            cell.peopleL.text = self.heroVideomodel.A;
        }
        return cell;
    }
    return nil;
}

#pragma 点击collectionView弹出隐藏tableView方法
- (void)clickHeroImageV:(NSString *)mid{
    [UIView animateWithDuration:0.3 animations:^{
        [self createHeroTableView];
        [self getHeroVideoData];
       
        self.heroTableV.frame = CGRectMake(0, 300 * SCREENHEIGHT / HEIGHTSIXP, SCREENWIDTH,  (SCREENHEIGHT -  300)* SCREENHEIGHT / HEIGHTSIXP);
    }];
}

#pragma 获取collectionView的英雄数据
- (void)getHeroData{
    self.heroArr = [NSMutableArray array];
    NSString *url = @"http://v.wanmeixx.com/Ajax20151126.ashx?g=1&act=GetHeroList";
    [LQCAFNetTool getNetWithURL:url body:nil headFile:nil responseStyle:LQCJSON success:^(NSURLSessionDataTask *task, id responseObject) {
        NSMutableDictionary *dic = responseObject;
        NSMutableArray *arr = [dic objectForKey:@"Data"];
        self.heroArr = [UFOheroModel baseModelWithArr:arr];
        [self.tableV reloadData];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
    }];
}

#pragma 创建弹出的tableView
- (void)createHeroTableView{
    self.heroTableV = [[UITableView alloc] initWithFrame:CGRectMake(0, 667 * SCREENHEIGHT / HEIGHTSIXP, SCREENWIDTH, SCREENHEIGHT) style:UITableViewStylePlain];
    self.heroTableV.backgroundColor = [UIColor colorWithRed:0.262 green:0.077 blue:0.500 alpha:1.000];
    self.heroTableV.delegate = self;
    self.heroTableV.dataSource = self;
    [self.view addSubview:self.heroTableV];
    [self.heroTableV release];
}

#pragma 点击回收弹出的tableView按钮
- (void)blowBAction:(UIButton *)sender{
    /**
     *  弹出tableView动画,收回tableView动画
     */
    [UIView animateWithDuration:0.3 animations:^{
        
        self.heroTableV.frame = CGRectMake(0, 667 * SCREENHEIGHT / HEIGHTSIXP, SCREENWIDTH, SCREENHEIGHT);
    }completion:^(BOOL finished) {
        [self.heroTableV removeFromSuperview];
        
    }];
}

#pragma 获取弹出tableView的视频数据
- (void)getHeroVideoData{

    self.heroVideoArr = [NSMutableArray array];
    NSString *url = [NSString stringWithFormat:@"http://v.wanmeixx.com/Ajax20151126.ashx?g=1&act=GetVideoList&heroId=%@&authorId=0&keyword=&page=1&pageSize=10&order=new&albumId=0&isIpad=0",self.Hmodel.mId];
    
    [LQCAFNetTool getNetWithURL:url body:nil headFile:nil responseStyle:LQCJSON success:^(NSURLSessionDataTask *task, id responseObject) {
        
        NSMutableDictionary *dic = responseObject;
        NSMutableDictionary *dataDic = [dic objectForKey:@"Data"];
        NSMutableArray *arr = [dataDic objectForKey:@"Result"];
        for (NSMutableDictionary *tempDic in arr) {
            self.heroVideomodel = [[UFOheroVideoModel alloc] initWithDic:tempDic];
            [self.heroVideoArr addObject:self.heroVideomodel];
        }
        [self.heroTableV reloadData];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
    }];
}

#pragma 点击获取视频资源并播放
- (void)getPlayheroData{
    NSString *url = [NSString stringWithFormat:@"http://v.wanmeixx.com/ajax20151126.ashx?act=GetYouKuUrls&id=%@&format=3gphd",self.heroVideomodel.I];
    [LQCAFNetTool getNetWithURL:url body:nil headFile:nil responseStyle:LQCJSON success:^(NSURLSessionDataTask *task, id responseObject) {
        
        NSMutableDictionary *dic = responseObject;
        NSMutableArray *arr = [dic objectForKey:@"Data"];
        self.heroVideoPlayArr = [UFOheroVideoPlayModel baseModelWithArr:arr];
        self.play = [[self.heroVideoPlayArr objectAtIndex:0] Url];
        [self changeVideoUrl];
        [self.player play];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
    }];
    
}

- (void)getNewVIdeoData{
    NSString *url = [NSString stringWithFormat:@"http://v.wanmeixx.com/ajax20151126.ashx?act=GetYouKuUrls&id=%@&format=3gphd",self.heroVideomodel.I];
    [LQCAFNetTool getNetWithURL:url body:nil headFile:nil responseStyle:LQCJSON success:^(NSURLSessionDataTask *task, id responseObject) {
        
        NSMutableDictionary *dic = responseObject;
        NSMutableArray *arr = [dic objectForKey:@"Data"];
        self.hotVideoPlayArr = [UFOheroVideoPlayModel baseModelWithArr:arr];
        self.play = [[self.hotVideoPlayArr objectAtIndex:0] Url];
        [self changeVideoUrl];
        [self.player play];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
    }];
}

#pragma 最新视频
- (void)getNewData{
    self.hotVideoArr = [NSMutableArray array];
    NSString *url = @"http://v.wanmeixx.com/Ajax20151126.ashx?g=1&act=GetVideoList&heroId=0&authorId=0&keyword=&page=1&pageSize=10&order=week&albumId=0&isIpad=0";
    
    [LQCAFNetTool getNetWithURL:url body:nil headFile:nil responseStyle:LQCJSON success:^(NSURLSessionDataTask *task, id responseObject) {
        
        NSMutableDictionary *dic = responseObject;
        NSMutableDictionary *dataDic = [dic objectForKey:@"Data"];
        NSMutableArray *arr = [dataDic objectForKey:@"Result"];
        for (NSMutableDictionary *tempDic in arr) {
            self.heroVideomodel = [[UFOheroVideoModel alloc] initWithDic:tempDic];
            [self.hotVideoArr addObject:self.heroVideomodel];
        }
        [self.tableV reloadData];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
